{% extends "school/layout.html" %}

{% set page_title = translate('教师账号管理') %}

{% block content %}
<style>
	#changePhone{
		display: none;
	}
</style>
<div class="m-content">
	<form id="teacherNewForm" class="form-horizontal" method="POST">
		{{ form.hidden_tag() }}
		<fieldset class="fieldset">
			<legend>{{translate('新建教师')}}</legend>
			{% include "school/teacher_form_common.html" %}
		</fieldset>

		<fieldset class="fieldset">
			<legend>{{translate('账号信息')}}</legend>
			<div class="form-group">
				<label class="control-label col-sm-2">{{ form.username.label(class_="norequired") }}:</label>
				<div class="col-sm-5 input-group account-special">
					<span class="input-group-addon" id='school_code'>{{ current_user.school.code }}.</span>
					{{ form.username(class_="form-control", pattern="^[a-zA-z]\w{3,16}$", required=True) }}
					{% if form.username.errors %}
	        		<span class="text-danger">{{ translate(form.username.errors[0]) }}</span>
	    			{% endif %}
				</div>

			</div>
			<div>
				<button id="save" class="btn btn-primary">{{translate('保存')}}</button>
			</div>

		</fieldset>
	</form>
</div>
{% endblock %}

{% block js %}
<script type="text/javascript">
	var countDown = 60;
    var isCounting = false;
    var t;
    var labels = $('#teacherNewForm').find('label.control-label');
	$(function(){
		for(var i = 0; i < labels.length; i++){
			if(labels[i].htmlFor!=='nick_name'){
				$(labels[i]).prepend('<span class="dot-tips">*</span>')
			}
		}

		 function setTime(val){
	      if(isCounting){
	          val.attr('disabled','disabled');
	          val.addClass('btn-disable');
	          val.html('{{translate("在")}}'+countDown+'{{translate("秒后重新发送")}}');
	          countDown--;
	          if(countDown<0){
	              isCounting = false;
	              countDown = 60;
	          }
	      }else{
	          val.removeAttr('disabled');
	          val.removeClass('btn-disable');
	          val.html('{{translate("发送验证码")}}');
	          clearTimeout(t);
	          return false;
	      }
	      t = setTimeout(function(){
	          setTime(val);
	      },1050);
	    }

		$('#sendVerify').click(function(e){
			e.preventDefault();
			var phone = $('#phone').val();
			var $this = $(this);
			var pReg = new RegExp('^[0-9]{11}$');
			if(pReg.test(phone)){
				$.post('/school/teachers/check_phone',{'phone':phone},function(data){
					var da = JSON.parse(data);
					if(da.code == 0){
						$.post('/vcode/send_vcode',{'phone':phone},function(data){
						var d = JSON.parse(data);
						if(d.meta.code == 0){
							isCounting = true;
		                    setTime($this);
						}else{
							if(d.meta.code == 1){
								kalert('{{translate("验证码不正确，请重新输入")}}');
							}else if(d.meta.code == 2){
								kalert('{{translate("发验证码失败，请稍后再试")}}');
							}
						}
					});
					}else{
						kalert('{{translate("输入的手机号已经被占用，请检查后重新输入！")}}')
					}
				})
			}else{
				kalert('{{translate("输入的手机号不符合规范！")}}')
			}
		});
	});
</script>
{% endblock %}
